package com.coldlz.mmp.proxy;

import org.slf4j.Logger;

import java.net.SocketAddress;

public class LogUtil {

    public static void info(Logger log, UserContext userContext, String msg) {
        if (userContext == null) {
            log.info("{}", msg);
        } else {
            int sessionId = userContext.getSessionId();
            SocketAddress sourceAddress = userContext.getClientChannel().remoteAddress();
            SocketAddress targetAddress = userContext.getRemoteChannel().remoteAddress();
            log.info("【{}->{}】{} {}", sourceAddress, targetAddress, sessionId, msg);
        }
    }

    public static void warn(Logger log, UserContext userContext, String msg) {
        if (userContext == null) {
            log.warn("{}", msg);
        } else {
            int sessionId = userContext.getSessionId();
            SocketAddress sourceAddress = userContext.getClientChannel().remoteAddress();
            SocketAddress targetAddress = userContext.getRemoteChannel().remoteAddress();
            log.warn("【{}->{}】{} {}", sourceAddress, targetAddress, sessionId, msg);
        }
    }

    public static void error(Logger log, UserContext userContext, String msg) {
        if (userContext == null) {
            log.error("{}", msg);
        } else {
            int sessionId = userContext.getSessionId();
            SocketAddress sourceAddress = userContext.getClientChannel().remoteAddress();
            SocketAddress targetAddress = userContext.getRemoteChannel().remoteAddress();
            log.error("【{}->{}】{} {}", sourceAddress, targetAddress, sessionId, msg);
        }
    }

    public static void error(Logger log, UserContext userContext, String msg, Throwable cause) {
        if (userContext == null) {
            log.error("{}", msg, cause);
        } else {
            int sessionId = userContext.getSessionId();
            SocketAddress sourceAddress = userContext.getClientChannel().remoteAddress();
            SocketAddress targetAddress = userContext.getRemoteChannel().remoteAddress();
            log.error("【{}->{}】{} {}", sourceAddress, targetAddress, sessionId, msg, cause);
        }
    }
}
